//
//  SettingViewController.swift
//  PDF2PNG
//
//  Created by Jarvis on 12/05/2017.
//  Copyright © 2017 Jarvis. All rights reserved.
//

import UIKit
import LeanCloudFeedback

class SettingViewController: TableViewController {

    let cellID = "settingCellID"
    
    var items = [String]()
    
    override func viewDidLoad() {
        super.viewDidLoad()

        self.title = "Setting".localized()
        view.backgroundColor = UIColor.white

        // 在cellForRow中创建
//        tableView.register(UITableViewCell.self, forCellReuseIdentifier: cellID)
        
        items.append("Review in the App Store".localized())
        items.append("Feedback".localized())
        items.append("Version".localized())
        
        tableView.reloadData()
    }
    
    
    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return items.count
    }
    
    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        
        var cell = tableView.dequeueReusableCell(withIdentifier: cellID)
        if cell == nil {
            cell = UITableViewCell(style: .value1, reuseIdentifier: cellID)
        }
        
        cell?.textLabel?.text = items[indexPath.row]
        
        switch indexPath.row {
        case 0:
            cell?.accessoryType = .disclosureIndicator
            break
        case 1:
            cell?.accessoryType = .disclosureIndicator
            break
        case 2:
            cell?.accessoryType = .none
            
            let version =  Bundle.main.infoDictionary!["CFBundleShortVersionString"] as! String
            
            cell?.detailTextLabel?.text = String(format: "v%@", version)
            break
        default:
            break
        }
        
        return cell!
    }
    

    override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        tableView.deselectRow(at: indexPath, animated: true)
        
        switch indexPath.row {
        case 0:
            let appURL = URL(string: "https://itunes.apple.com/us/app/pdf2img-convert-a-pdf-file-into-a-long-picture/id1236146668?ls=1&mt=8")
            if UIApplication.shared.canOpenURL(appURL!) {
                UIApplication.shared.openURL(appURL!)
            }
            
            break
        case 1:
//            navigationController?.pushViewController(FeedbackViewController(), animated: true)
//            let agent = LCUserFeedbackAgent.sharedInstance()
//            agent?.showConversations(self, title: "Feedback", contact: "Email or Messenger")
            
            let feedback = LCUserFeedbackViewController()
            feedback.contact = "Email or Messenger"
            feedback.presented = false
            feedback.navigationBarStyle = LCUserFeedbackNavigationBarStyleNone
            feedback.title = "Feedback"
            self.navigationController?.pushViewController(feedback, animated: true)
            
            break
        case 2:
            let alert = UIAlertController(title: "Alert".localized(), message: "The current version is up-to-date".localized(), preferredStyle: .alert)
            alert.addAction(UIAlertAction(title: "OK", style: .cancel, handler: nil))
            present(alert, animated: true, completion: nil)
            break
        default:
            break
        }
    }
    
    
}
